home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-19 / iritsm3s.zip / CAGDCOER.C < prev    next >
C/C++ Source or Header  |  1992-01-28  |  8KB  |  215 lines

  1. /******************************************************************************
  2. * CagdCoer.c - Handle point coercesions/conversions.                  *
  3. *******************************************************************************
  4. * Written by Gershon Elber, Aug. 90.                          *
  5. ******************************************************************************/
  6.  
  7. #include "cagd_loc.h"
  8.  
  9. /******************************************************************************
  10. * Coerce Srf/Crv Point from index Index of Points array of Type PType to E2   *
  11. * If however Index < 0 Points is considered single point.              *
  12. ******************************************************************************/
  13. void CagdCoerceToE2(CagdRType *E2Point, CagdRType *Points[CAGD_MAX_PT_SIZE],
  14.                             int Index, CagdPointType PType)
  15. {
  16.     CagdBType
  17.     IsRational = CAGD_IS_RATIONAL_PT(PType);
  18.     int i,
  19.         MaxCoord = CAGD_NUM_OF_PT_COORD(PType);
  20.     CagdRType *Point;
  21.  
  22.     if (MaxCoord > 2) MaxCoord = 2;
  23.  
  24.     if (Index < 0) {                  /* Points is one single point. */
  25.         Point = *Points;
  26.     for (i = 1; i <= MaxCoord; i++)
  27.         *E2Point++ = IsRational ? Point[i] / Point[W] : Point[i];
  28.     }
  29.     else                         /* Points is a full arrays from Srf or Crv. */
  30.     for (i = 1; i <= MaxCoord; i++)
  31.         *E2Point++ = IsRational ? Points[i][Index] / Points[W][Index] :
  32.                       Points[i][Index];
  33.  
  34.     for (i = MaxCoord + 1; i <= 2; i++)
  35.     *E2Point++ = 0.0;
  36. }
  37.  
  38. /******************************************************************************
  39. * Coerce Srf/Crv Point from index Index of Points array of Type PType to E3   *
  40. * If however Index < 0 Points is considered single point.              *
  41. ******************************************************************************/
  42. void CagdCoerceToE3(CagdRType *E3Point, CagdRType *Points[CAGD_MAX_PT_SIZE],
  43.                         int Index, CagdPointType PType)
  44. {
  45.     CagdBType
  46.     IsRational = CAGD_IS_RATIONAL_PT(PType);
  47.     int i,
  48.     MaxCoord = CAGD_NUM_OF_PT_COORD(PType);
  49.     CagdRType *Point;
  50.  
  51.     if (MaxCoord > 3) MaxCoord = 3;
  52.  
  53.     if (Index < 0) {                  /* Points is one single point. */
  54.     Point = *Points;
  55.     for (i = 1; i <= MaxCoord; i++)
  56.         *E3Point++ = IsRational ? Point[i] / Point[W] : Point[i];
  57.     }
  58.     else                         /* Points is a full arrays from Srf or Crv. */
  59.     for (i = 1; i <= MaxCoord; i++)
  60.         *E3Point++ = IsRational ? Points[i][Index] / Points[W][Index] :
  61.                       Points[i][Index];
  62.  
  63.     for (i = MaxCoord + 1; i <= 3; i++)
  64.     *E3Point++ = 0.0;
  65. }
  66.  
  67. /******************************************************************************
  68. * Coerce Srf/Crv Point from index Index of Points array of Type PType to P2   *
  69. * If however Index < 0 Points is considered single point.              *
  70. ******************************************************************************/
  71. void CagdCoerceToP2(CagdRType *P2Point, CagdRType *Points[CAGD_MAX_PT_SIZE],
  72.                             int Index, CagdPointType PType)
  73. {
  74.     CagdBType
  75.     IsRational = CAGD_IS_RATIONAL_PT(PType);
  76.     int i,
  77.         MaxCoord = CAGD_NUM_OF_PT_COORD(PType);
  78.     CagdRType *Point;
  79.  
  80.     if (MaxCoord > 2) MaxCoord = 2;
  81.  
  82.     if (Index < 0) {                  /* Points is one single point. */
  83.         Point = *Points;
  84.     *P2Point++ = IsRational ? Point[W] : 1.0;
  85.     for (i = 1; i <= MaxCoord; i++)
  86.         *P2Point++ = Point[i];
  87.     }
  88.     else {                       /* Points is a full arrays from Srf or Crv. */
  89.     *P2Point++ = IsRational ? Points[W][Index] : 1.0;
  90.     for (i = 1; i <= MaxCoord; i++)
  91.         *P2Point++ = Points[i][Index];
  92.     }
  93.  
  94.     for (i = MaxCoord + 1; i <= 2; i++)
  95.     *P2Point++ = 0.0;
  96. }
  97.  
  98. /******************************************************************************
  99. * Coerce Srf/Crv Point from index Index of Points array of Type PType to P3   *
  100. * If however Index < 0 Points is considered single point.              *
  101. ******************************************************************************/
  102. void CagdCoerceToP3(CagdRType *P3Point, CagdRType *Points[CAGD_MAX_PT_SIZE],
  103.                         int Index, CagdPointType PType)
  104. {
  105.     CagdBType
  106.     IsRational = CAGD_IS_RATIONAL_PT(PType);
  107.     int i,
  108.     MaxCoord = CAGD_NUM_OF_PT_COORD(PType);
  109.     CagdRType *Point;
  110.  
  111.     if (MaxCoord > 3) MaxCoord = 3;
  112.  
  113.     if (Index < 0) {                  /* Points is one single point. */
  114.     Point = *Points;
  115.     *P3Point++ = IsRational ? Point[W] : 1.0;
  116.     for (i = 1; i <= MaxCoord; i++)
  117.         *P3Point++ = Point[i];
  118.     }
  119.     else {                       /* Points is a full arrays from Srf or Crv. */
  120.     *P3Point++ = IsRational ? Points[W][Index] : 1.0;
  121.     for (i = 1; i <= MaxCoord; i++)
  122.         *P3Point++ = Points[i][Index];
  123.     }
  124.  
  125.     for (i = MaxCoord + 1; i <= 3; i++)
  126.     *P3Point++ = 0.0;
  127. }
  128.  
  129. /******************************************************************************
  130. * Coerce Points array of point type OldPType to point type NewPType, in place *
  131. ******************************************************************************/
  132. void CagdCoercePointsTo(CagdRType *Points[], int Len,
  133.             CagdPointType OldPType, CagdPointType NewPType)
  134. {
  135.     int i, j,
  136.     OldIsRational = CAGD_IS_RATIONAL_PT(OldPType),
  137.     OldNumOfCoords = CAGD_NUM_OF_PT_COORD(OldPType),
  138.     NewIsRational = CAGD_IS_RATIONAL_PT(NewPType),
  139.     NewNumOfCoords = CAGD_NUM_OF_PT_COORD(NewPType);
  140.     CagdRType *NewPoints[CAGD_MAX_PT_SIZE], Pt[CAGD_MAX_PT_SIZE];
  141.     
  142.  
  143.     for (i = !NewIsRational; i <= NewNumOfCoords; i++)
  144.     NewPoints[i] = (CagdRType *) CagdMalloc(sizeof(CagdRType) * Len);
  145.  
  146.     for (i = 0; i < Len; i++) {
  147.     switch (NewPType) {
  148.         case CAGD_PT_E2_TYPE:
  149.             CagdCoerceToE2(Pt, Points, i, OldPType);
  150.             break;
  151.         case CAGD_PT_E3_TYPE:
  152.             CagdCoerceToE3(Pt, Points, i, OldPType);
  153.             break;
  154.         case CAGD_PT_P2_TYPE:
  155.             CagdCoerceToP2(Pt, Points, i, OldPType);
  156.             break;
  157.         case CAGD_PT_P3_TYPE:
  158.             CagdCoerceToP3(Pt, Points, i, OldPType);
  159.             break;
  160.         default:
  161.         FATAL_ERROR(CAGD_ERR_UNSUPPORT_PT);
  162.         break;
  163.     }
  164.  
  165.     if (NewIsRational)
  166.         for (j = 0; j <= NewNumOfCoords; j++) NewPoints[j][i] = Pt[j];
  167.     else
  168.         for (j = 1; j <= NewNumOfCoords; j++) NewPoints[j][i] = Pt[j - 1];
  169.     }
  170.  
  171.     /* Replace old rep. with new. */
  172.     for (i = !OldIsRational; i <= OldNumOfCoords; i++)
  173.     CagdFree((VoidPtr) Points[i]);
  174.     for (i = !NewIsRational; i <= NewNumOfCoords; i++)
  175.     Points[i] = NewPoints[i];
  176.     for (; i <= CAGD_MAX_PT_COORD; i++)
  177.     Points[i] = NULL;
  178. }
  179.  
  180. /******************************************************************************
  181. * Coerce Crv to point type PType.                          *
  182. ******************************************************************************/
  183. CagdCrvStruct *CagdCoerceCrvTo(CagdCrvStruct *Crv, CagdPointType PType)
  184. {
  185.     Crv = CagdCrvCopy(Crv);
  186.     CagdCoercePointsTo(Crv -> Points, Crv -> Length, Crv -> PType, PType);
  187.     Crv -> PType = PType;
  188.     return Crv;
  189. }
  190.  
  191. /******************************************************************************
  192. * Coerce Srf to point type PType.                          *
  193. ******************************************************************************/
  194. CagdSrfStruct *CagdCoerceSrfTo(CagdSrfStruct *Srf, CagdPointType PType)
  195. {
  196.     Srf = CagdSrfCopy(Srf);
  197.     CagdCoercePointsTo(Srf -> Points, Srf -> ULength * Srf -> VLength,
  198.                Srf -> PType, PType);
  199.     Srf -> PType = PType;
  200.     return Srf;
  201. }
  202.  
  203. /******************************************************************************
  204. * Returns the point which includes the two provided.                  *
  205. ******************************************************************************/
  206. CagdPointType CagdMergePointType(CagdPointType PType1, CagdPointType PType2)
  207. {
  208.     CagdBType
  209.     IsRational = CAGD_IS_RATIONAL_PT(PType1) || CAGD_IS_RATIONAL_PT(PType2);
  210.     int NumCoords = MAX(CAGD_NUM_OF_PT_COORD(PType1),
  211.                 CAGD_NUM_OF_PT_COORD(PType2));
  212.  
  213.     return CAGD_MAKE_PT_TYPE(IsRational, NumCoords);
  214. }
  215.